package fr.utbm.to52.android_bibtex.controller;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import fr.utbm.to52.android_bibtex.R;

public class EntryListAdapter extends BaseAdapter {

	private static ArrayList<ListViewItem> mItems;
	private final LayoutInflater mInflater;

	public EntryListAdapter(Context context, ArrayList<ListViewItem> items) {
		mItems = items;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	static class ViewHolder {
		TextView title;
		TextView author;
		TextView type;
		TextView date;
		TextView key;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = this.mInflater.inflate(R.layout.entry_list_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.entryTitle);
			holder.author = (TextView) convertView.findViewById(R.id.entryAuthor);
			holder.type = (TextView) convertView.findViewById(R.id.entryType);
			holder.date = (TextView) convertView.findViewById(R.id.entryDate);
			holder.key = (TextView) convertView.findViewById(R.id.entryKey);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.title.setText(mItems.get(position).getTitle());
		holder.author.setText(mItems.get(position).getAuthor());
		holder.type.setText(mItems.get(position).getType());
		holder.date.setText(mItems.get(position).getYear());
		holder.key.setText(mItems.get(position).getKey());

		return convertView;
	}

}
